/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.SysUserStore;
import com.yuhanginfo.modules.sys.service.SysUserStoreService;

/**
 * 用户门店关联Controller
 * @author lvls
 * @version 2017-09-20
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/sysUserStore")
public class SysUserStoreController extends BaseController {

	@Autowired
	private SysUserStoreService sysUserStoreService;
	
	@ModelAttribute
	public SysUserStore get(@RequestParam(required=false) String id) {
		SysUserStore entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysUserStoreService.get(id);
		}
		if (entity == null){
			entity = new SysUserStore();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:sysUserStore:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysUserStore sysUserStore, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SysUserStore> page = sysUserStoreService.findPage(new Page<SysUserStore>(request, response), sysUserStore); 
		model.addAttribute("page", page);
		return "modules/sys/sysUserStoreList";
	}

	@RequiresPermissions("sys:sysUserStore:view")
	@RequestMapping(value = "form")
	public String form(SysUserStore sysUserStore, Model model) {
		model.addAttribute("sysUserStore", sysUserStore);
		return "modules/sys/sysUserStoreForm";
	}

	@RequiresPermissions("sys:sysUserStore:edit")
	@RequestMapping(value = "save")
	public String save(SysUserStore sysUserStore, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sysUserStore)){
			return form(sysUserStore, model);
		}
		sysUserStoreService.save(sysUserStore);
		addMessage(redirectAttributes, "保存用户门店关联成功");
		return "redirect:"+Global.getAdminPath()+"/sys/sysUserStore/?repage";
	}
	
	@RequiresPermissions("sys:sysUserStore:edit")
	@RequestMapping(value = "delete")
	public String delete(SysUserStore sysUserStore, RedirectAttributes redirectAttributes) {
		sysUserStoreService.delete(sysUserStore);
		addMessage(redirectAttributes, "删除用户门店关联成功");
		return "redirect:"+Global.getAdminPath()+"/sys/sysUserStore/?repage";
	}

}